<?php

namespace app\admin\controller;

use think\admin\Controller;
use think\admin\service\AdminService;
use think\admin\service\MenuService;

/**
 * 后台界面入口
 * Class Index
 * @package app\admin\controller
 */
class Index extends Controller
{

    /**
     * 显示后台首页
     * @return \think\response\Redirect|void|null
     * @throws \ReflectionException
     * @throws \think\admin\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        if ($this->isMobile()) {
            $showId = request()->get('show_id');
            if (request()->get('show', '') == 'operate') {
                return redirect(getHttpHost() . "/data/record.car_record_operate/edit.html?id={$showId}");
            } else if (request()->get('show', '') == 'docking') {
                return redirect(getHttpHost() . "/data/record.car_record_docking/edit.html?id={$showId}");
            } else if (request()->get('show', '') == 'quality_requirements') {
                // http://youkejia.test/data/rework.rework_answer_question/qualityRequirements.html?show=quality_requirements
                return redirect(getHttpHost() . "/data/rework.rework_answer_question/qualityRequirements.html");
            } else if (request()->get('show', '') == 'answer_question') {
                return redirect(getHttpHost() . "/data/rework.rework_answer_question/answerQuestion.html");
            } else if (request()->get('show', '') == 'answer_question_success') {
                return redirect(getHttpHost() . "/data/rework.rework_answer_question/answerQuestionSuccess.html");
            } else if (request()->get('show', '') == 'answer_question_fail') {
                return redirect(getHttpHost() . "/data/rework.rework_answer_question/answerQuestionFail.html");
            } else if (request()->get('show', '') == 'car_record_complete') {
                $id = request()->get('id');
                return redirect(getHttpHost() . "/data/record.car_record_complete/edit.html?id={$id}");
            } else {
                return redirect(getHttpHost() . "/data/record.car_record_city_owner/edit.html?id={$showId}");
            }
        } else {
            /*! 根据运行模式刷新权限 */
            AdminService::instance()->apply($this->app->isDebug());
            /*! 读取当前用户权限菜单树 */
            $this->menus = MenuService::instance()->getTree();
            /*! 判断当前用户的登录状态 */
            $this->login = AdminService::instance()->isLogin();
            /*! 菜单为空且未登录跳转到登录页 */
            if (empty($this->menus) && empty($this->login)) {
                $this->redirect(sysuri('admin/login/index'));
            } else {
                $this->title = '系统管理后台';
                $this->isSuper = AdminService::instance()->isSuper();
                $this->theme = AdminService::instance()->getUserTheme();
                $this->fetch();
            }
        }
    }

    /**
     * 判断是否手机端
     * @return bool
     */
    protected function isMobile()
    {
        $userAgent = request()->header('User-Agent');
        $isMobile = false;

        if (preg_match('/(iPhone|iPad|iPod|Android|webOS|BlackBerry|Windows Phone)/i', $userAgent)) {
            $isMobile = true;
        }

        return $isMobile;
    }

    /**
     * 后台主题切换
     * @login true
     * @return void
     * @throws \think\admin\Exception
     */
    public function theme()
    {
        if ($this->request->isGet()) {
            $this->theme = AdminService::instance()->getUserTheme();
            $this->themes = Config::themes;
            $this->fetch();
        } else {
            $data = $this->_vali(['site_theme.require' => '主题名称不能为空！']);
            if (AdminService::instance()->setUserTheme($data['site_theme'], AdminService::instance()->getUserId())) {
                $this->success('主题配置保存成功！');
            } else {
                $this->error('主题配置保存失败！');
            }
        }
    }

    /**
     * 修改用户资料
     * @login true
     * @param mixed $id 用户ID
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info($id = 0)
    {
        $this->_applyFormToken();
        if (AdminService::instance()->getUserId() === intval($id)) {
            $this->_form('WorkUser', 'admin@user/form', 'id', [], ['id' => $id]);
        } else {
            $this->error('只能修改自己的资料！');
        }
    }

    /**
     * 资料修改后处理
     * @param bool $status
     */
    protected function _info_form_result(bool $status)
    {
        if ($status) {
            $this->success('用户资料修改成功！', 'javascript:location.reload()');
        }
    }

    /**
     * 修改当前用户密码
     * @login true
     * @param mixed $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pass($id = 0)
    {
        $this->_applyFormToken();
        if (AdminService::instance()->getUserId() !== intval($id)) {
            $this->error('只能修改当前用户的密码！');
        }
        if ($this->app->request->isGet()) {
            $this->verify = true;
            $this->_form('WorkUser', 'admin@user/pass', 'id', [], ['id' => $id]);
        } else {
            $data = $this->_vali([
                'password.require'            => '登录密码不能为空！',
                'repassword.require'          => '重复密码不能为空！',
                'oldpassword.require'         => '旧的密码不能为空！',
                'password.confirm:repassword' => '两次输入的密码不一致！',
            ]);
            $user = $this->app->db->name('WorkUser')->where(['id' => $id])->find();
            if (md5($data['oldpassword']) !== $user['password']) {
                $this->error('旧密码验证失败，请重新输入！');
            }
            if (data_save('WorkUser', ['id' => $user['id'], 'password' => md5($data['password'])])) {
                sysoplog('系统用户管理', "修改用户[{$user['id']}]密码成功");
                $this->success('密码修改成功，下次请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }

}
